﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Restricted_Usersite : System.Web.UI.Page
{
    static aspnet_User currentUser = new aspnet_User();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack != true)
        {
            DateiaftenDataContext db = new DateiaftenDataContext();
            currentUser = db.aspnet_Users.SingleOrDefault(x => x.UserName == Membership.GetUser().UserName);

            FillPersonContent();
            DateProfilePicture.ImageUrl = setProfilePicture(currentUser.UserName);
            DescriptionTxt.Text = currentUser.Description;
        }
    }

    private void FillPersonContent()
    {
        //Fill in first and last name
        FirstnameTxt.Text = currentUser.FirstName;
        LastnameTxt.Text = currentUser.LastName;

        //Fill in Birthdate
        for (int i = DateTime.Today.Year - 100; i < DateTime.Today.Year - 15; i++)
        {
            ListItem ages = new ListItem();
            ages.Text = i.ToString();
            AgeYearDDL.Items.Add(ages);
        }
        AgeYearDDL.SelectedIndex = currentUser.Age.Value.Year - (DateTime.Today.Year - 100);

        string[] months = { "Jan", "Feb", "Mar", "Apr", "Maj", "Jun", "Jul", "Aug", "Sep", "Okt", "Nov", "Dec" };

        foreach (var item in months)
        {
            ListItem mon = new ListItem();
            mon.Text = item;
            AgeMonthDDL.Items.Add(mon);
        }
        AgeMonthDDL.SelectedIndex = currentUser.Age.Value.Month -1;


        for (int i = 1; i < 32; i++)
        {
            ListItem days = new ListItem();
            days.Text = i.ToString();
            AgeDayDDL.Items.Add(days);
        }
        AgeDayDDL.SelectedIndex = currentUser.Age.Value.Day - 1;

        CityZipcodeTxt.Text = currentUser.City.Cityname + " (" + currentUser.City.Zipcode + ")";


        //Fill hair color DDL
        string[] HairColors = { "Ikke angivet", "Sort", "Blond", "Brun", "Rød", "Grå", "Skaldet" };
        foreach (var item in HairColors)
        {
            ListItem mon = new ListItem();
            mon.Text = item;
            HaircolorDDL.Items.Add(mon);
        }
        if (currentUser.Haircolor != null)
        {
            HaircolorDDL.SelectedIndex = currentUser.Haircolor.Value;
        }


        //Fill in eye color DDL
        string[] EyeColor = { "Ikke angivet", "Grøn", "Blå", "Brun", "Grå", "Rød" };

        foreach (var item in EyeColor)
        {
            ListItem mon = new ListItem();
            mon.Text = item;
            EyecolorDDL.Items.Add(mon);
        }
        if (currentUser.Eyecolor != null)
        {
            EyecolorDDL.SelectedIndex = currentUser.Eyecolor.Value;
        }
        

        //Fill in conformation DDL
        string[] Confir = { "Ikke angivet", "Spinkel", "Slank", "Almindelig", "Robust", "Atletisk", "Tonet", "Buttet", "Overvægtig" };

        foreach (var item in Confir)
        {
            ListItem mon = new ListItem();
            mon.Text = item;
            ConformationDDL.Items.Add(mon);
        }
        if (currentUser.Conformation != null)
        {
            ConformationDDL.SelectedIndex = currentUser.Conformation.Value;
        }

        //Fill in Height DDL
        for (int i = 100; i < 251; i++)
        {
            ListItem days = new ListItem();
            days.Text = i.ToString();
            HeightDDL.Items.Add(days);
        }
        if (currentUser.Height != null)
        {
            HeightDDL.SelectedIndex = currentUser.Height.Value;
        }

        //Fill in Weight DDL
        for (int i = 40; i < 201; i++)
        {
            ListItem days = new ListItem();
            days.Text = i.ToString();
            WeightDDL.Items.Add(days);
        }
        if (currentUser.Weight != null)
        {
            WeightDDL.SelectedIndex = currentUser.Weight.Value;
        }

        if (currentUser.Smoker != null)
        {
            SmokerDDL.SelectedIndex = currentUser.Smoker.Value;
        }
    }

    private string setProfilePicture(string username)
    {
        string path = Server.MapPath("../images/ProfilePictures/");
        if (File.Exists(path + username + ".png"))
        {
            return "../images/ProfilePictures/" + username + ".png";
        }
        else
        {
            return "../images/ProfilePictures/" + "default.png";
        }
    }

    [System.Web.Services.WebMethod]
    [System.Web.Script.Services.ScriptMethod]
    public static string[] GetCompletionListCity(string prefixText, int count, string contextKey)
    {
        return DatenightBLL.Instance.getCitysForCompletionlist(prefixText).ToArray();
    }

    protected void UploadButton_Click(object sender, EventArgs e)
    {
        if (FileUploadControl.HasFile)
        {
            try
            {
                if (FileUploadControl.PostedFile.ContentType == "image/jpeg" || FileUploadControl.PostedFile.ContentType == "image/png")
                {
                    if (FileUploadControl.PostedFile.ContentLength < 1002400)
                    {
                        string extension = Path.GetExtension(FileUploadControl.PostedFile.FileName);
                        string filename = Membership.GetUser().UserName;
                        FileUploadControl.SaveAs(Server.MapPath("~/images/ProfilePictures/") + filename + extension);
                        StatusLabel.Text = "Billedet blev overført!";
                    }
                    else
                        StatusLabel.Text = "Billedet skal være mindre end 1 MB";
                }
                else
                    StatusLabel.Text = "Billedet skal være PNG eller JPEG filformat";
            }
            catch (Exception ex)
            {
                StatusLabel.Text = "Billedet blev ikke overført. Følgende fejl skete: " + ex.Message;
            }
        }
    }

    protected void SaveChanges_Click(object sender, EventArgs e)
    {
        DateiaftenDataContext db = new DateiaftenDataContext();
        currentUser = db.aspnet_Users.SingleOrDefault(x => x.UserName == Membership.GetUser().UserName);

        if (FirstnameTxt.Text != "" || LastnameTxt.Text != "")
        {
            currentUser.FirstName = FirstnameTxt.Text;
            currentUser.LastName = LastnameTxt.Text;
        }
        else
        {
            WarningLabel.Text = "For- og Efternavn skal udfyldes";
        }
        DateTime age = new DateTime(Convert.ToInt32(AgeYearDDL.SelectedValue), AgeMonthDDL.SelectedIndex +1, AgeDayDDL.SelectedIndex +1);
        currentUser.Age = age;

        string cityname = CityZipcodeTxt.Text;
        cityname = cityname.Substring(0, cityname.Length - 7);
        string zipcode = CityZipcodeTxt.Text;
        zipcode = zipcode.Substring(zipcode.Length - 5, 4);

        
        var CityObj = db.Cities.SingleOrDefault(x => x.Cityname == cityname && x.Zipcode == Convert.ToInt32(zipcode));
        currentUser.CityId = CityObj.CityId;

        if (NewcodeTxt.Text != "" || ReNewcodeTxt.Text != "" || NewcodeTxt.Text == ReNewcodeTxt.Text)
        {
            
        }

        currentUser.Haircolor = HaircolorDDL.SelectedIndex;
        currentUser.Eyecolor = EyecolorDDL.SelectedIndex;
        currentUser.Conformation = ConformationDDL.SelectedIndex;
        currentUser.Height = HeightDDL.SelectedIndex;
        currentUser.Weight = WeightDDL.SelectedIndex;
        currentUser.Smoker = SmokerDDL.SelectedIndex;
        currentUser.Description = DescriptionTxt.Text;

        db.SubmitChanges();
    }
}